import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

//路由的懒加载
let register = () => import("../pages/register/register.vue")
let login = () => import("../pages/login/login.vue")
let index = () => import("../pages/index/index.vue")
let list = () => import("../pages/list/list.vue")
let addressList = () => import("../pages/address/addressList.vue")
let addAddress = () => import("../pages/address/addAddress.vue")
let confirmOrder = () => import("../pages/confirmOrder/confirmOrder.vue")
let pay = () => import("../pages/pay/pay.vue")

let home = () => {
  return Promise.resolve(import("../pages/home/home.vue"))
}
let cate = () => {
  return Promise.resolve(import("../pages/cate/cate.vue"))
}

let shop = () => Promise.resolve(import("../pages/shop/shop.vue"))
let mine = () => Promise.resolve(import("../pages/mine/mine.vue"))
const routes = [
  //一级路由规则
  {
    path: "/login",
    components: {
      default: login, //出现在默认的路由视图中
      // second: test //出现在了叫second的路由视图中
    },
    // 元信息 meta 
    //给当前这条路由携带一些信息的
    meta: {
      title: "登录"
    }
  },
  {
    path: "/register",
    component: register, //出现在默认的路由出口中
    meta: {
      title: "注册",
      goBack: true
    }
  },
  {
    path: "/confirmOrder",
    component: confirmOrder, //出现在默认的路由出口中
    meta: {
      title: "确认订单",
      goBack: true
    }
  },
  {
    path: "/addressList",
    component: addressList, //出现在默认的路由出口中
    meta: {
      title: "地址管理",
      goBack: true
    }
  },
  {
    path: "/addAddress",
    component: addAddress, //出现在默认的路由出口中
    meta: {
      title: "新增地址",
      goBack: true
    }
  },
  {
    path: "/pay",
    component: pay, //出现在默认的路由出口中
    meta: {
      title: "支付页面",
      goBack: true
    }
  },
  {
    path: "/index",
    component: index,
    //二级路由规则
    children: [{
        path: "home",
        component: home,
        meta: {
          title: "首页"
        }
      },
      {
        path: "cate",
        component: cate,
        meta: {
          title: "分类"
        }
      },
      {
        path: "shop",
        component: shop,
        meta: {
          title: "购物车"
        }
      },
      {
        path: "mine",
        component: mine,
        meta: {
          title: "会员中心"
        },
      },
      //二级重定向
      {
        path: "",
        redirect: "home"
      }
    ]
  },
  {
    path: "/detail/:id",
    component: () => import("../pages/detail/detail.vue"),
    meta: {
      title: "商品详情",
      goBack: true
    },
    
  },
  {
    path: "/list",
    component: list,
    meta: {
      title: "商品列表",
      goBack: true
    }
  },
  {
    path: "/search",
    //别名
    alias: "/s",
    //命名路由
    name: "搜索",
    component: () => import("../pages/search/search.vue"),
    meta: {
      title: "搜索",
      goBack: true
    }
  },
  //重定向
  {
    path: "*",
    redirect: "/login"
  }
]

const router = new VueRouter({
  // 路由模式
  mode:"hash",//默认hash(有#) history(无#)


  //路由规则
  routes,
  //滚动行为
  /*
  没有加scrollBehavior，默认情况是：
    home上卷了100，跳到list，list直接先上卷了100，但是我们希望list是从(0,0)的位置开始展示。
  所以可以通过scrollBehavior解决以上问题。

    如果上一次保存了位置，就返回上一次的位置；如果没有保存，就返回(0,0)
  
  */
  /**
   * 
   * @param {*} to 去的新的路由的路由信息
   * @param {*} from  从哪里来的路由信息
   * @param {*} savePosition 做返回的时候，上一次离开该路由存的位置信息
   * @returns 
   */
  scrollBehavior(to, from, savePosition) {
    // console.group();
    // console.log("to:",to);
    // console.log("from:",from);
    // console.log("savePosition：",savePosition);
    // console.groupEnd()
    // return {x:0,y:0}

    if (savePosition) {
      return savePosition
    } else {
      return {
        x: 0,
        y: 0
      }
    }
  }
  
})



// 全局前置守卫
/**
 * to: 前往的路由信息
 * from:从哪里来的路由信息
 * next: 是否允许进入
 */
router.beforeEach((to,from,next)=>{
  // console.log("全局前置守卫");
  // 1.如果你要去的是登录或者注册，进入；
  if(to.path==="/login"||to.path=="/register"){
    next()
    return
  }

  // 2.不是登录|注册， 判断是不是登录了
  let isLogin=localStorage.getItem("isLogin");// '1'  null
  // 登录了，进入
  if(isLogin){
    next()
    return;
  }
  // 未登录，进入登录页
  next("/login")
})

//全局后置守卫:所有的路由进入之后
// router.afterEach((to,from)=>{
//   console.log("======全局后置守卫=======");
// })



export default router